package com.jxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxj.dao.AddrTimeMapper;
import com.jxj.dao.DepartmentMapper;
import com.jxj.dao.RecruitMapper;
import com.jxj.entity.RecruitVO;
import com.jxj.entity.gen.TAddrtime;
import com.jxj.entity.gen.TDepartmentMsg;
import com.jxj.entity.gen.TRecruitMsg;
import com.jxj.service.RecruitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Cambria
 * @creat 2021/8/14 23:08
 */
@Slf4j
@Service
public class RecruitServiceImpl extends ServiceImpl<RecruitMapper , TRecruitMsg> implements RecruitService {
    @Autowired
    RecruitMapper recruitMapper;
    @Autowired
    AddrTimeMapper addrTimeMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public RecruitVO getRecruitMsgByRid(String rid) {
        TRecruitMsg tRecruitMsg = recruitMapper.selectById(rid);

        RecruitVO vo = new RecruitVO();
        vo.setTitle(tRecruitMsg.getTitle());
        vo.setImg(tRecruitMsg.getImgPath());
        vo.setOid(String.valueOf(tRecruitMsg.getOId()));
        vo.setVideo(tRecruitMsg.getVideoPath());
        vo.setQrcode(tRecruitMsg.getQrcodePath());
        vo.setIntroduce(tRecruitMsg.getIntroduce());

        QueryWrapper<TAddrtime> addrtimeQueryWrapper = new QueryWrapper<>();
        addrtimeQueryWrapper.eq("rId" , rid);
        log.debug("select addr&time of rid {}" , rid);
        List<TAddrtime> addrtimes = addrTimeMapper.selectList(addrtimeQueryWrapper);
        log.debug("result:{}" , addrtimes);

        vo.setTimeaddr(addrtimes);

        QueryWrapper<TDepartmentMsg> departmentMsgQueryWrapper = new QueryWrapper<>();
        departmentMsgQueryWrapper.eq("oId" , tRecruitMsg.getOId());
        List<TDepartmentMsg> departmentMsgs = departmentMapper.selectList(departmentMsgQueryWrapper);
        List<String> depMsgVO = new ArrayList<>();
        for (TDepartmentMsg departmentMsg : departmentMsgs) {
            depMsgVO.add(departmentMsg.getDepartment() + ":" + departmentMsg.getPersonNum());
        }
        vo.setDepartments(depMsgVO);

        return vo;
    }
}
